using System;
using System.Collections.Generic;
using System.Text;
using Entities;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace DAL
{
    public class tbTrangThaiSPDAL
    {
        private SqlConnection cn = null;
        public tbTrangThaiSPDAL()
        {
            cn = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
            cn.Open();
        }
        public void Insert(tbTrangThaiSP trangthaisp)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiSP_Insert", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@TenTrangThai",trangthaisp.TenTrangThai);
            cm.ExecuteNonQuery();
        }
        public void Update(tbTrangThaiSP trangthaisp)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiSP_Update", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTrangThaiSP", trangthaisp.IdTrangThaiSP);
            cm.Parameters.AddWithValue("@TenTrangThai", trangthaisp.TenTrangThai);
            cm.ExecuteNonQuery();
        }
        public void Delete(int Id)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiSP_Delete", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@IdTrangThaiSP", Id);
            cm.ExecuteNonQuery();
        }
        public tbTrangThaiSPCollection Get(string Where, int PageIndex, int PageSize, string OrderBy, string OrderDirection, out int TotalRecords)
        {
            SqlCommand cm = new SqlCommand("sp_TrangThaiSP_Get", cn);
            cm.CommandType = System.Data.CommandType.StoredProcedure;
            cm.Parameters.AddWithValue("@Where", Where);
            cm.Parameters.AddWithValue("@PageIndex", PageIndex);
            cm.Parameters.AddWithValue("@PageSize", PageSize);
            cm.Parameters.AddWithValue("@OrderBy", OrderBy);
            cm.Parameters.AddWithValue("@OrderDirection", OrderDirection);
            cm .Parameters .Add (new SqlParameter ("@TotalRecords",DbType .Int32 )).Direction =ParameterDirection .Output ;
            SqlDataReader dr = cm.ExecuteReader();
            tbTrangThaiSPCollection ttspCollection = new tbTrangThaiSPCollection();
            while (dr.Read())
            {
                tbTrangThaiSP ttsp = new tbTrangThaiSP();
                ttsp.IdTrangThaiSP = int.Parse(dr.GetValue(0).ToString());
                ttsp.TenTrangThai = dr.GetValue(1).ToString();
                ttspCollection.Add(ttsp);
            }
            dr.Close();
            TotalRecords = int.Parse(cm.Parameters["@TotalRecords"].Value.ToString());
            return ttspCollection;
        }
    }
}
